package com.quanyue.mechanics.complex.controller;

import com.quanyue.auto.util.ServiceResult;
import com.quanyue.auto.web.Scope;
import com.quanyue.mechanics.complex.service.PurposeService;
import com.quanyue.mechanics.domain.constant.UserRole;
import com.quanyue.mechanics.domain.entity.Purpose;
import com.vhuomai.mybatis.mapper.page.Page;
import com.vhuomai.mybatis.mapper.page.PageRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

@Api(tags = "联系我们")
@RestController
@RequestMapping("/api")
public class PurposeController {
    @Autowired
    private PurposeService purposeService;

    @ApiOperation(value = "添加留言")
    @PostMapping(value = "/purpose", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Purpose> addPurpose(@RequestBody Purpose purpose) {
        return ServiceResult.success(purposeService.addPurpose(purpose));
    }

    @ApiOperation(value = "修改留言")
    @PutMapping(value = "/purpose", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> updatePurpose(@RequestBody Purpose purpose) {
        purposeService.updatePurpose(purpose);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "删除留言")
    @Scope(role = UserRole.MANAGER)
    @DeleteMapping(value = "/purpose", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> deletePurpose(@RequestParam("id") Integer id) {
        purposeService.deletePurpose(id);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "留言列表")
    @PostMapping(value = "/purpose/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Page <Purpose>> findPurpose(@RequestBody PageRequest req) {
        return ServiceResult.success(purposeService.findPurpose(req));
    }
}
